﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class PositionController : Controller
    {
        //
        // GET: /Position/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// get positions list
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPosition()
        {
            string searchPos = Request.Params["query"];
            PositionContainer container = PositionModels.GetPosArray(searchPos);
            return Json(container, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// add new position
        /// </summary>
        /// <returns></returns>
        public ActionResult AddPosition()
        {
            string position = Request.Params["data"];
            string messageBack = null;

            PositionModels checkPosition = new PositionModels();
            checkPosition = PositionModels.CheckPosition(position);

            try
            {
                if (position != "")
                {
                    if (checkPosition == null)
                    {
                        PositionModels.AddPosition(position);
                    }
                    else
                    {
                        messageBack = "Functia exista deja";
                    }
                }

				return Json(new { success = true, description = messageBack });
            }
            catch (Exception ex)
            {
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume";
				}
				return Json(new { success = true, description = messageBack });
            }
        }

    }
}
